import 'package:dio/dio.dart';

class HttpRequestUtil {
  //静态属性
  static final HttpRequestUtil _singleUtil = HttpRequestUtil._init();
  //工厂方法
  factory HttpRequestUtil() {
    return _singleUtil;
  }

  Dio _dio;
  String _baseUrl;
  String _ApiAppKey;

  HttpRequestUtil._init() {
    if (_dio == null) {
//      Options options = Options();
//      options.responseType = ResponseType.json;
      _dio = new Dio();
    }
    if (_baseUrl==null){
      _baseUrl ="http://api.tianapi.com/";
    }
    if (_ApiAppKey==null){
      _ApiAppKey ="bf3221c04935ae1bf362afcb9f34d2de";
    }
  }

  Future<Map> get(String url, {Map<String, dynamic> param}) async {
    Response<Map<String,dynamic>> response;
    url = _baseUrl +url +"?key=$_ApiAppKey";
    if (param!=null&&param is Map){
      response = await _dio.get(url, queryParameters: param);
    }
    else{
      response = await _dio.get(url);
    }
    print("url = $url");
    print("response.data = ${response.data}");

    return response.data;
  }

}
